/*
 * CeldaSDL.cpp
 *
 *  Created on: 23/10/2013
 *      Author: fernando
 */

#include "CeldaSDL.h"
#include <string>
#include "SDL.h"


using std::string;


const char IMAGEN_CELDA_ACTIVA[] = "Imagenes/Celda.png";
const char IMAGEN_CELDA_INACTIVA[] = "Imagenes/CeldaNoDisponible.png";

CeldaSDL::CeldaSDL() {
	estado = false;
	relleno = NULL;
}

CeldaSDL::~CeldaSDL() {
	if (!isVacia()){
		delete relleno;
	}else{
		SDL_FreeSurface(background);
	}
}

CeldaSDL::CeldaSDL(bool value, unsigned int x, unsigned int y,
		unsigned int num){
	estado = value;
	posX = x;
	posY = y;
	relleno = NULL;
	numero = num;
	setBackground();
}

CeldaSDL::CeldaSDL(Golosina* golo, unsigned int x, unsigned int y,
		unsigned int num){
	relleno = golo;
	posX = x;
	posY = y;
	estado = true;
	numero = num;
	setBackground();
}

bool CeldaSDL::setGolosina(Golosina* golo){
	if (estado){
		relleno = golo;
		return true;
	}
	return false;
}

bool CeldaSDL::setPosicion(unsigned int x, unsigned int y){
	if (estado){
		posX = x;
		posY = y;
		return true;
	}
	return false;
}

void CeldaSDL::setBackground(){
	if (!estado){
		background = SDL::load_image(IMAGEN_CELDA_INACTIVA);
	}
}

void CeldaSDL::dibujar(SDL_Surface* screen, bool destruirGolosina){
	if (!estado){
		SDL::apply_surface(posX, posY, background, screen, NULL);
	}else{
		dibujarGolosina(screen, destruirGolosina);
	}
}

void CeldaSDL::dibujarGolosina(SDL_Surface* screen, bool destruir){
	if (estado && relleno != NULL){
		relleno->dibujar(screen, destruir);
	}
}

Golosina* CeldaSDL::getGolosina(){
	return relleno;
}

const unsigned int CeldaSDL::getPosX(){
	return posX;
}

const unsigned int CeldaSDL::getPosY(){
	return posY;
}

const bool CeldaSDL::getEstado(){
	return estado;
}

void CeldaSDL::vaciar(){
	relleno = NULL;
}

bool CeldaSDL::isVacia(){
	return (relleno == NULL);
}

const unsigned int CeldaSDL::getNumero(){
	return numero;
}

bool CeldaSDL::operator ==(CeldaSDL& c){
	return (this->getPosX()== c.getPosX() && this->getPosY() == c.getPosY());
}

void CeldaSDL::destruirContenido(){
	Golosina* tmp = relleno;
	relleno = NULL;
	delete tmp;
}
